package com.mmtech.edb.data.service.impl;

import com.mmtech.edb.data.model.User;
import com.mmtech.edb.data.service.DataService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 实际的数据服务
 *
 * @author wuchong
 * @since 2019-07-15 14:45
 **/
@Service
public class DataServiceImpl implements DataService<User> {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public DataServiceImpl(@Qualifier("primaryJdbcTemplate") JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<User> produce(String bizType, String startDate, String endDate) {

        try (InputStream inputStream = new ClassPathResource("sql/getCreditUser.sql").getInputStream()) {
            String flowSql = IOUtils.toString(inputStream, Charset.defaultCharset());
            return jdbcTemplate.query(flowSql, new Object[]{startDate, endDate}, new BeanPropertyRowMapper<>(User.class));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
